package cn.bupt.adt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.dao.autogenerate.DoctorAppUserMapper;
import cn.bupt.adt.dao.autogenerate.DoctorViewMapper;
import cn.bupt.adt.dao.autogenerate.InstitutionAppUserMapper;
import cn.bupt.adt.dao.autogenerate.InstitutionViewMapper;
import cn.bupt.adt.dao.autogenerate.WithdrawMapper;
import cn.bupt.adt.entity.autogenerate.DoctorAppUser;
import cn.bupt.adt.entity.autogenerate.DoctorAppUserExample;
import cn.bupt.adt.entity.autogenerate.DoctorView;
import cn.bupt.adt.entity.autogenerate.DoctorViewExample;
import cn.bupt.adt.entity.autogenerate.InstitutionAppUser;
import cn.bupt.adt.entity.autogenerate.InstitutionView;
import cn.bupt.adt.entity.autogenerate.InstitutionViewExample;
import cn.bupt.adt.entity.autogenerate.Withdraw;
import cn.bupt.adt.entity.autogenerate.WithdrawExample;
import cn.bupt.adt.model.WithdrawModel;
import cn.bupt.adt.service.IWithdrawService;
import cn.bupt.adt.util.BeanUtil;
@Service
public class WithdrawService implements IWithdrawService {

	@Autowired
	WithdrawMapper withdrawMapper;
	
	@Autowired 
	DoctorAppUserMapper doctorAppUserMapper;
	
	@Autowired 
	InstitutionAppUserMapper institutionAppUserMapper;
	
	@Autowired
	DoctorViewMapper doctorViewMapper;
	
	@Autowired
	InstitutionViewMapper institutionViewMapper;
	@Override
	public boolean addWithdraw(Withdraw withdraw, DoctorAppUser doc) {
		//减少余额，增加待提现金额，更新医生字段
		doc.setMoney(doc.getMoney()-withdraw.getMoney());
		doc.setWaitedToBeWithdrawnMoney(doc.getWaitedToBeWithdrawnMoney()+withdraw.getMoney());
		int i = doctorAppUserMapper.updateByPrimaryKeySelective(doc);
		//添加提现记录
		int j = withdrawMapper.insert(withdraw);
		return i == 1 && j == 1;
	}
	@Override
	public boolean addWithdraw(Withdraw withdraw, InstitutionAppUser ins) {
		ins.setMoney(ins.getMoney()-withdraw.getMoney());
		int i = institutionAppUserMapper.updateByPrimaryKeySelective(ins);
		int j = withdrawMapper.insert(withdraw);
		return i == 1 && j == 1;
	}
	@Override
	public List<WithdrawModel> getAllWithdraws() {
		List<Withdraw> withdraws = withdrawMapper.selectByExample(new WithdrawExample());
/*		List<WithdrawModel> withdrawModels = new ArrayList<WithdrawModel>(withdraws.size());
		DoctorViewExample dex = new DoctorViewExample();
		InstitutionViewExample iex = new InstitutionViewExample();
		for(Withdraw w:withdraws) {
			WithdrawModel wm = new WithdrawModel();
			try {
				BeanUtil.fatherToChild(w, wm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(w.getFlag().equals(Constants.DOC_WITHDRAW)) {
				dex.clear();
				dex.createCriteria().andAppIdEqualTo(w.getUserId());
				DoctorView doc = doctorViewMapper.selectByExample(dex).get(0);
				wm.setName(doc.getName());
				wm.setPhone(doc.getPhone());
			}
			else {
				iex.clear();
				iex.createCriteria().andIdEqualTo(w.getUserId());
				InstitutionView ins = institutionViewMapper.selectByExample(iex).get(0);
				wm.setName(ins.getName());
				wm.setPhone(ins.getPhone());
			}
			withdrawModels.add(wm);
		}*/
		return transferModel(withdraws);
	}
	@Override
	public Withdraw getWithdrawById(int id) {
		return withdrawMapper.selectByPrimaryKey(id);
	}
	@Override
	public boolean updateWithdrawStatus(Withdraw w) {
		return withdrawMapper.updateByPrimaryKeySelective(w) == 1;
	}
	private List<WithdrawModel> transferModel(List<Withdraw> withdraws) {
		List<WithdrawModel> withdrawModels = new ArrayList<WithdrawModel>(withdraws.size());
		InstitutionViewExample iex = new InstitutionViewExample();
		for(Withdraw w:withdraws) {
			WithdrawModel wm = new WithdrawModel();
			try {
				BeanUtil.fatherToChild(w, wm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(w.getFlag().equals(Constants.DOC_WITHDRAW)) {
				DoctorAppUser doc = doctorAppUserMapper.selectByPrimaryKey(w.getUserId());
				wm.setName(doc.getName());
				wm.setPhone(doc.getPhone());
			}
			else {
				iex.clear();
				iex.createCriteria().andIdEqualTo(w.getUserId());
				List<InstitutionView> lists = institutionViewMapper.selectByExample(iex);
				InstitutionView ins = lists.get(0);
				wm.setName(ins.getName());
				wm.setPhone(ins.getPhone());
			}
			withdrawModels.add(wm);
		}
		return withdrawModels;
	}
	@Override
	public List<WithdrawModel> getUndealedWithdraws() {
		WithdrawExample ex = new WithdrawExample();
		ex.createCriteria().andDealedEqualTo(Constants.Withdraw_Undealed);
		List<Withdraw> withdraws = withdrawMapper.selectByExample(ex);
/*		List<WithdrawModel> withdrawModels = new ArrayList<WithdrawModel>(withdraws.size());
		DoctorViewExample dex = new DoctorViewExample();
		InstitutionViewExample iex = new InstitutionViewExample();
		for(Withdraw w:withdraws) {
			WithdrawModel wm = new WithdrawModel();
			try {
				BeanUtil.fatherToChild(w, wm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(w.getFlag().equals(Constants.DOC_WITHDRAW)) {
				dex.clear();
				dex.createCriteria().andAppIdEqualTo(w.getUserId());
				DoctorView doc = doctorViewMapper.selectByExample(dex).get(0);
				wm.setName(doc.getName());
				wm.setPhone(doc.getPhone());
			}
			else {
				iex.clear();
				iex.createCriteria().andIdEqualTo(w.getUserId());
				InstitutionView ins = institutionViewMapper.selectByExample(iex).get(0);
				wm.setName(ins.getName());
				wm.setPhone(ins.getPhone());
			}
			withdrawModels.add(wm);
		}*/
		return transferModel(withdraws);
	}

}
